package org.clockwise.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class MyFileWriter {
	
	private String filename;
	private FileWriter writer;
	
	public MyFileWriter(String filename, boolean isOverwrite) {		
		File file = new File(filename);
		if (!file.exists()) {
			file.mkdirs();
		}
		if (file.isDirectory()) {
			file.delete();
		}
		
		this.filename = filename;
		try {
			writer = new FileWriter(filename, !isOverwrite);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Cannot open file \"" + filename + "\"");
		}
	}
	
	public synchronized void write(String s) {
			try {
			//	System.out.println("writing: " + s);
				writer.write(s);
				writer.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("Cannot write to file \"" + filename + "\"");
			}
	}
	
	public void close() {
		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Cannot close file \"" + filename + "\"");
		}
	}
	
}
